package com.sicurezza.nfcrypto;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.progettoSicurezza.db.KeyContactContract.KeyContactEntry;
import com.progettoSicurezza.db.KeyContactDbHelper;
import com.progettoSicurezza.model.Person;
import com.progettoSicurezza.model.PersonAdapter;

public class MainActivity extends Activity implements LoaderCallbacks<Cursor>{
	
	private SharedPreferences sp = this.getSharedPreferences("KeyPreferences", MODE_PRIVATE);
	private final static String TAG = "MainActivity";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		SharedPreferences sp = this.getSharedPreferences("KeyPreferences", MODE_PRIVATE);
		boolean logged = sp.getBoolean("logged", false);
		String mp = sp.getString("MasterPassword", null);
		KeyContactDbHelper kcdh = new KeyContactDbHelper(this);
		ArrayList<Person> personFromDB = new ArrayList<Person>();
		PersonAdapter personAdp = new PersonAdapter(this, personFromDB);
		
		if(mp.equalsIgnoreCase(null)){
			Log.w(TAG, "MasterPassword non definita");
			Intent mpIntent = new Intent();
			mpIntent.setClassName("com.progettoSicurezza.nfcrypto","MasterPasswordActivity");
			startActivity(mpIntent);
		}
		
		if(!logged){
			Log.w(TAG, "Utente non loggato");
			Intent loginIntent = new Intent();
			loginIntent.setClassName("com.progettoSicurezza.nfcrypto", "LoginActivity");
			startActivity(loginIntent);
		}
		
		SQLiteDatabase db = kcdh.getReadableDatabase();
		Cursor c = db.query(KeyContactEntry.TABLE_NAME, new String[]{KeyContactEntry.CONTACT_ID,KeyContactEntry.CONTACT_DISPLAY_NAME, KeyContactEntry.CONTACT_PHONE_NUMBER, KeyContactEntry.CONTACT_EMAIL_ADDRESS,KeyContactEntry.KEY_HASH_VALUE,KeyContactEntry.SALT_STRING_VALUE}, null, null, null, null, KeyContactEntry.CONTACT_DISPLAY_NAME + " DESC");
		if(c.moveToFirst()){
			while(c.moveToNext()){
				Person toAdd = new Person();
				toAdd.setId(c.getString(c.getColumnIndex(KeyContactEntry.CONTACT_ID)));
				toAdd.setDisplayName(c.getString(c.getColumnIndex(KeyContactEntry.CONTACT_DISPLAY_NAME)));
				toAdd.setPhoneNumber(c.getString(c.getColumnIndex(KeyContactEntry.CONTACT_PHONE_NUMBER)));
				toAdd.setEmailAddress(c.getString(c.getColumnIndex(KeyContactEntry.CONTACT_EMAIL_ADDRESS)));
				toAdd.setKeyHashValue(c.getString(c.getColumnIndex(KeyContactEntry.KEY_HASH_VALUE)));
				toAdd.setSaltStringValue(c.getString(c.getColumnIndex(KeyContactEntry.SALT_STRING_VALUE)));
				personFromDB.add(toAdd);
			}
		}
		else{ Log.e(TAG, "No Result from DB");}
		
		ListView contactListView = (ListView) findViewById(R.id.listView1);
		contactListView.setAdapter(personAdp);
		contactListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Person selected = (Person) parent.getItemAtPosition(position);
				
				Intent contactViewIntent = new Intent(getApplicationContext(), ContactViewActivity.class);
//				contactViewIntent.putExtra(KeyContactEntry.CONTACT_ID, selected.getId());
				contactViewIntent.putExtra(KeyContactEntry.CONTACT_DISPLAY_NAME, selected.getDisplayName());
				contactViewIntent.putExtra(KeyContactEntry.CONTACT_PHONE_NUMBER, selected.getPhoneNumber());
				contactViewIntent.putExtra(KeyContactEntry.CONTACT_EMAIL_ADDRESS, selected.getEmailAddress());
				contactViewIntent.putExtra(KeyContactEntry.KEY_HASH_VALUE, selected.getKeyHashValue());
				contactViewIntent.putExtra(KeyContactEntry.SALT_STRING_VALUE, selected.getSaltStringValue());
				startActivity(contactViewIntent);
				
			}
		});
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main, menu);
		return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(item.getItemId() == R.id.text_view_start){
			Intent addContactIntent = new Intent();
			addContactIntent.setClassName("com.progettoSicurezza.nfcrypto", "KeyCreationActivity");
			this.startActivity(addContactIntent);
		}
		return super.onOptionsItemSelected(item);
	}
	
	@Override
	protected void onDestroy() {
		SharedPreferences.Editor spe = sp.edit();
		spe.putBoolean("logged", false);
		Log.v(TAG, "Utente sloggato");
		spe.commit();
		super.onDestroy();
	}

	@Override
	public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onLoaderReset(Loader<Cursor> arg0) {
		// TODO Auto-generated method stub
		
	}

}
